﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace Historia.Formulaires
{
    /// <summary>
    /// Interaction logic for frmFil.xaml
    /// </summary>
    public partial class frmFil : Window
    {
        #region Membres

        protected DataManager _dm = null;
        protected Int32? _idFil = null;
        protected DataRow _dr = null;

        #endregion

        #region Constructeur

        public frmFil()
        {
            InitializeComponent();
        }

       
        #endregion

        #region Méthodes

        public bool? ShowFormDialog(ref DataManager dataManager, Int32? idFil)
        {
            this._dm = dataManager;
            this._idFil = idFil;
            this.InitData();
            this.LoadData();
            this.DisplayData();
            return this.ShowDialog();
        }

        protected void InitData() {
            if (this._idFil.HasValue)
            {
                this._dr = this._dm.DB.Tables[DataManager.FILS].Select("id_fil = " + this._idFil.ToString()).First();
            }
            else {
                this._dr = this._dm.DB.Tables[DataManager.FILS].NewRow();
            }
        }

        protected void LoadData() {
            
        }

        protected void DisplayData() {
            this.txtNom.Text = this._dr.Field<string>("nom");
             this.txtAbreviation.Text = this._dr.Field<string>("abreviation");
            this.txtCouleur.Text = this._dr.Field<string>("couleur");
            
        }

        protected void ReadData() {
            
            this._dr.SetField<string>("nom", this.txtNom.Text);
            this._dr.SetField<string>("abreviation", this.txtAbreviation.Text);
            this._dr.SetField<string>("couleur", this.txtCouleur.Text);
                        
        }

        #endregion

        private void btnAnnuler_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = false;
                this._dm.DB.Tables[DataManager.FILS].RejectChanges();
                this.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ReadData();
                if (!this._idFil.HasValue)
                {
                    this._dm.DB.Tables[DataManager.FILS].Rows.Add(this._dr);
                }
                this._dm.DB.Tables[DataManager.FILS].AcceptChanges();
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

    }
}
